package io.gitee.xyuesheng.day06;

import com.sun.javafx.scene.control.skin.LabeledImpl;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class TestNestMap {
    @Test
    public void testReadFile() throws IOException {
//        //读文件
//        List<String> strings=Files.readAllLines(Paths.get("data/scores2.csv"));
//
//        //把每一行数据转化成score对象
//        List<Score> scores=new ArrayList<>();
//        for(String line:strings){
//            String[] split=line.split(",");
//            Score score=new Score(split[0],split[1],Double.parseDouble(split[2]));
//            scores.add(score);
//        }
        List<Score> scores = loadScoresFromFile();
        System.out.println(scores);
    }

    public List<Score> loadScoresFromFile() throws IOException {
        //读文件
        List<String> strings = Files.readAllLines(Paths.get("data/scores2.csv"),
                Charset.forName("gbk"));
        //文件编码设置成gbk

        //把每一行数据转化成score对象
        List<Score> scores = new ArrayList<>();

        //跳过第一行:姓名,课程,分数(分数不为double类型)
        for (int i = 1; i < strings.size(); i++) {
            String[] split = strings.get(i).split(",");
//        for(String line:strings){
//            String[] split=line.split(",");
            Score score = new Score(split[0], split[1], Double.parseDouble(split[2]));
            scores.add(score);
        }
        return scores;
    }

    //读取文件，将学生的名字和分数封装到map集合
    public Map<String, List<Score>> loadScores() throws IOException {
//        List<Score>=》Map<String,list>
        Map<String, List<Score>> map = new HashMap<>();
        List<Score> scores = loadScoresFromFile();
        for (Score score : scores) {
            String name = score.getName();

            //map里面已经有当前学生的分数
            if (map.containsKey(name)) {
//                System.out.println(name);
                //将学生分数加入List集合
                map.get(name).add(score);
            } else {
                //存放当前学生的所有分数
                List<Score> studentScores = new ArrayList<>();
                studentScores.add(score);
                map.put(name, studentScores);
            }
        }
        return map;
    }

    /**
     * 根据学生的姓名查找他所有成绩
     */
    public List<Score> getScoresByName(String name) throws IOException {
        Map<String, List<Score>> map = loadScores();

        return map.get(name);
    }

    @Test
    public void test() throws IOException {
        List<Score> scores = getScoresByName("张三");
//        System.out.println(scores.size());
        //张三有三门课的成绩
        Assert.assertEquals(3, scores.size());

        Score s1 = new Score("张三", "语文", 55.5);
//        Score s2=new Score("张三","数学",100);
//        Score s3=new Score("张三","物理",99);
//        Score s4=new Score("张三","生物",52);
//
//        Assert.assertTrue(scores.contains(s1));
//        Assert.assertTrue(scores.contains(s2));
//        Assert.assertTrue(scores.contains(s3));
//        //张三没有s4这个分数记录
//        Assert.assertFalse(scores.contains(s4));
//
//        //赵六不存在
//        Assert.assertNull(getScoreByName("赵六"));
    }

    /**
     * 查找某个学生、某门课的成绩
     */
    public Score getScore(String name, String course) throws IOException {
//        Map<String, List<Score>> map= loadScores();
        List<Score> scores = loadScoresFromFile();

        //外层map，key是名字，value是个map
        //内层map，key是课程，value是课程对应的分数
        Map<String, Map<String, Score>> map = new HashMap<>();

        for (Score score : scores) {
            String key = score.getName();

            if (map.containsKey(key)) {
                map.get(key).put(score.getCourse(), score);
            } else {
                //key是课程，value是课程对应的分数
                Map<String, Score> innerMap = new HashMap<>();
                innerMap.put(score.getCourse(), score);

                map.put(key, innerMap);
            }
        }
        return map.get(name).get(course);
    }

    @Test
    public void test2() throws IOException {
        Score score = getScore("张三", "语文");

        Score expected = new Score("张三", "语文", 55.5);

        Assert.assertEquals(expected, score);
        Assert.assertNull(getScore("张三", "高数"));
    }

    @Test
    public void testSet() {
        //HashSet和HashMap里面的元素是无序的
        Set<String> set = new HashSet<>();
    }

}
